//
//  ZH_GoodsEvaluationViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/26.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_GoodsEvaluationViewController.h"
#import "GC_GoodsSegmentView.h"
#import "ZH_EvaluationInforTableViewCell.h"
#import "ZH_EvaluationImageTableViewCell.h"
#import "ZH_EvaluationGoodApplyTableViewCell.h"
#import "ZH_EvaluationInformationViewController.h"
#import "ZH_MallViewModel.h"
@interface ZH_GoodsEvaluationViewController ()<UITableViewDelegate, UITableViewDataSource, GC_GoodsSegmentDelegate>
///选择 View
@property (nonatomic, weak) GC_GoodsSegmentView *segmentView;
///当前选择 0：商品；1：详情  3：评价
@property (nonatomic, assign) NSInteger currentItem;
@property (nonatomic,weak)UITableView *tableView;
//viewModel
@property (nonatomic,strong)ZH_MallViewModel *viewModel;
@end

@implementation ZH_GoodsEvaluationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initUserInterface{
    self.navigationItem.titleView = self.segmentView;
    self.view.backgroundColor = kCommonBackgroudColor;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.viewModel.goodsListArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    [array addObjectsFromArray:self.viewModel.goodsListArray[section][@"detail"]];
    return 2 + array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        ZH_EvaluationInforTableViewCell *cell = [ZH_EvaluationInforTableViewCell cellWithTableView:tableView];
        [cell updateUI:self.viewModel.goodsListArray[indexPath.section]];
        return cell;
    }
    else if(indexPath.row == 1){
        ZH_EvaluationImageTableViewCell *cell = [ZH_EvaluationImageTableViewCell cellWithTableView:tableView];
        [cell updateUiForData:self.viewModel.goodsListArray[indexPath.section][@"replay_img"]];
        [cell updateUiForDatas:self.viewModel.goodsListArray[indexPath.section]];
        return cell;
    }
    else{
        ZH_EvaluationGoodApplyTableViewCell *cell = [ZH_EvaluationGoodApplyTableViewCell cellWithTableView:tableView];
        NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
        [array addObjectsFromArray:self.viewModel.goodsListArray[indexPath.section][@"detail"]];
        [cell updataUI:array[indexPath.row - 2]];
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 1) {
        ZH_EvaluationInformationViewController *vc = [[ZH_EvaluationInformationViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - GC_GoodsSegmentDelegate
///当前选中项
- (void)customGoodsSegmentCurrentItem:(NSInteger)item
{
    //当前选项
    self.currentItem = item;
    //加载数据
    [self startLoadData];
}
- (void)startLoadData{
    [self.viewModel.goodsListParam setObject:self.idString forKey:@"id"];
    [self.viewModel.goodsListParam setObject:@(self.currentItem + 1) forKey:@"score"];
    NSLog(@"%@",self.viewModel.goodsListParam);
    [self.viewModel.goodsListArray removeAllObjects];
    WEAKSelf;
    [self.viewModel goodsListResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            [weakSelf showHint:message];
        }
    }];
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
///选择 View
- (GC_GoodsSegmentView *)segmentView
{
    if(!_segmentView){
        GC_GoodsSegmentView *view = [[GC_GoodsSegmentView alloc] initWithHeight:kNavBarHeight];
        //设置默认，选中字体
        [view setDefaultFont:kFontSize_36 selectFont:kFontSize_36];
        [view setDefaultFontColor:[UIColor colorWithHexString:@"#D9D9D9"] andSelectFontColor:kFontColorWhite];
        view.delegate = self;
        [view setUnLineItems:@[@"高分",@"低分"]];
        [view setTopLineImageViewHidden:YES];
        [view setBottomLineImageViewHidden:YES];
        [view setSelectItem:0];
        [self.view addSubview:_segmentView = view];
    }
    return _segmentView;
}
- (ZH_MallViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MallViewModel alloc] init];
    }
    return _viewModel;
}
@end
